﻿Imports System.Data.OleDb
Imports System.Text

Public Class readoledb

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Try
            txtResult.Text = ReadMyData(txtConString.Text.Trim, txtQueryString.Text.Trim)

        Catch ex As Exception
            MessageBox.Show(ex.StackTrace)
        End Try

        Button1.Enabled = True
    End Sub

    Public Function ReadMyData(ByVal connectionString As String, ByVal queryString As String) As String
        Dim tpsb As New stringbuilder

        Using connection As New OleDbConnection(connectionString)
            Dim command As New OleDbCommand(queryString, connection)
            connection.Open()
            Dim reader As OleDbDataReader = command.ExecuteReader()
            While reader.Read()

                Dim fieldc As Integer = reader.FieldCount


                For i As Integer = 0 To fieldc - 1
                    If reader(i) = "" Then
                        tpsb.Append("字段为空")
                    Else
                        tpsb.Append(reader(i).ToString())
                    End If
                    tpsb.Append("_")
                Next

                tpsb.Append(System.Environment.NewLine)

            End While ' always call Close when done reading.
            reader.Close()
        End Using

        Return tpsb.ToString()
    End Function
End Class